package com.huluobo.a2202mvvm_im_douyindemo.adapter;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.chat.ChatActivity;
import com.huluobo.a2202mvvm_im_douyindemo.chat.MyChatActivity;
import com.huluobo.a2202mvvm_im_douyindemo.contact.ContactListItemBean;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import java.util.List;

/**
 * Created by LC on 2024/4/25.
 */
public class ContactAdapter extends BaseQuickAdapter<ContactListItemBean, BaseViewHolder> {
    public ContactAdapter(@Nullable List<ContactListItemBean> data) {
        super(R.layout.item_contact, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, ContactListItemBean contactListItemBean) {
        baseViewHolder.setText(R.id.item_contact_username, contactListItemBean.getUsername());
        if (contactListItemBean.isShowFirstLetter()) {
            baseViewHolder.getView(R.id.item_contact_first_letter_tv).setVisibility(View.VISIBLE);
            baseViewHolder.setText(R.id.item_contact_first_letter_tv, contactListItemBean.getFirstLetter() + "");
        } else {
            baseViewHolder.getView(R.id.item_contact_first_letter_tv).setVisibility(View.GONE);
        }
        baseViewHolder.getView(R.id.item_contact_ll).setOnClickListener(v -> {
            String conversationId = contactListItemBean.getUsername();
            Intent intent = new Intent(getContext(), MyChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", 1);
            intent.putExtras(bundle);
            getContext().startActivity(intent);
        });

        baseViewHolder.getView(R.id.item_contact_ll).setOnLongClickListener(v -> {
            String message = String.format("要与%s友尽吗?", contactListItemBean.getUsername());
            new AlertDialog.Builder(getContext())
                    .setTitle("删除好友")
                    .setNegativeButton("取消", null)
                    .setMessage(message)
                    .setPositiveButton("确认", (dialog, which) -> deleteFriend(contactListItemBean.getUsername()))
                    .show();
            return true;
        });
    }

    private void deleteFriend(String username) {
        EMClient.getInstance().contactManager().aysncDeleteContact(username, new EMCallBack() {
            @Override
            public void onSuccess() {
                ToastUtils.showLong("删除成功");
            }

            @Override
            public void onError(int code, String error) {
                ToastUtils.showLong("删除失败" + error);
            }
        });
    }
}
